<?php
include 'common/autoload.php';
include 'common/header.html';
include 'common/left.html';
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

$w = " AND " . $_SESSION['memberID'];
$flag = false;
// $post_url = 'prod_edits.html?type=f';
$post_url = 'prod_edits_dt.php?type=f';
$str = "SELECT id, title,p_type,getPY(title,1) as py FROM uu_land "
    ."WHERE apply_did={$_SESSION['memberID']} AND status=1 ORDER BY py ASC";
$GLOBALS['le']->query($str);
$prods = array();
// while($row=$GLOBALS['le']->fetch_assoc()) {
    // $prods['1'][] = $row;
// }
$prods[1]=salePros($s,$_SESSION['sid']);
// 可分销产品


function salePros($s,$fid,$lid=Null){
	$r=array();
	$salePids=array();
	$str="select * from uu_priceset where pid=".$fid;
	$GLOBALS['le']->query($str);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$dprices[$row['tid']."_".$row['aid']]=$row['dprice'];
	}
	$sql="select p.id as pid,p.apply_did,p.p_status,l.id as lid,l.p_type,"
		."l.title,getPY(l.title,1) as py,l.area,l.topic,l.px,l.runtime,l.taobao_support,l.imgpath,"
		."l.address,t.id as tid,t.pay,t.getaddr,t.title as ttitle,t.tprice,t.notes,"
		."t.buy_limit_low,t.ddays,t.delaydays,t.status,f.ass_station,"
		."f.mdays,f.mhour,f.rdays from uu_products p left join uu_land l on p.contact_id=l.id left join
		uu_jq_ticket t on p.id=t.pid left join uu_land_f f on f.pid=p.id";
	$where="where p.p_status in (0,3,4,5) and p.apply_limit=1";
	if($lid) $where.=" and l.id=$lid ";
	$orderby=" order by l.px desc ";
		
	/*一手供应商产品*/
	$aids=array(0=>$fid);
	$str="select l.pids,l.aid from pft_product_sale_list l join pft_member m on l.aid=m.id where l.fid=".$fid." and l.status=0 and m.status=0";
	$GLOBALS['le']->query($str);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$salePids[$row['aid']]=explode(",",$row['pids']);
		$aids[]=$row['aid'];
	}
	if(count($aids)!=0){
		$aids=implode(",",$aids);
		$where1=$where." and p.apply_did in ($aids) ";
		$str1="$sql $where1 $orderby";
		$GLOBALS['le']->query($str1);
		while($row=$GLOBALS['le']->fetch_assoc()){
			if($row['apply_did']!=$fid&&$salePids[$row['apply_did']][0]!="A"&&!in_array($row['pid'],$salePids[$row['apply_did']])) continue;
			
			if(($row['jsprice']=$s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),1))==-1){
				$row['jsprice']=$s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),2);
			}
			if(($row['uprice']=$s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),1,"","",1))==-1){
				$row['uprice']=$s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),2,"","",1);
			}
			if($row['jsprice']==-1||$row['uprice']==-1) continue;
			$row['jsprice']=$row['jsprice']+$dprices[$row['pid']."_0"];
		
			$row['sapply_did']=$row['apply_did'];
			$row['aid']=0;
			$row['lvl']=0;
			$r[]=$row;
		}
	}
	
	/*二手供应商产品*/
	$str="select * from pft_p_apply_evolute where fid=".$fid." and status=0 and sid<>sourceid";
	$GLOBALS['le']->query($str);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$secondInfo[$row['pid']."_".$row['sid']]=$row;
		$secondPids[]=$row['pid'];
	}
	if(count($secondPids)!=0){
		$dprices2=array();
		$str="select * from uu_priceset where tid in (".implode(",",$secondPids).")";
		$GLOBALS['le']->query($str);
		while($row=$GLOBALS['le']->fetch_assoc()){
			$dprices2[$row['tid']."_".$row['pid']."_".$row['aid']]=$row['dprice'];
		}
		$proInfo=array();
		$secondPids=implode(",",$secondPids);
		$where2=$where." and p.id in ($secondPids) ";
		$str2="$sql $where2 $orderby";
		$GLOBALS['le']->query($str2);
		while($row=$GLOBALS['le']->fetch_assoc()){
			if(($row['jsprice']=$s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),1))==-1){
				$row['jsprice']=$s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),2);
			}
			if(($row['uprice']=$s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),1,"","",1))==-1){
				$row['uprice']=$s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),2,"","",1);
			}
			if($row['jsprice']==-1||$row['uprice']==-1) continue;
			$proInfo[$row['pid']]=$row;
		}
		foreach($secondInfo as $key=>$value){
			$key=explode("_",$key);
			$pid=$key[0];
			$sapply_did=$key[1];
			if(!$proInfo[$pid]) continue;
			$row=$proInfo[$pid];
			$row['sapply_did']=$sapply_did;
			$row['aid']=$sapply_did;
			$row['lvl']=$value['lvl'];
			$row['eid']=$value['id'];
			$row['estatus']=$value['status'];
			
			$aids=explode(",",$value['aids']);
	
			$aids[]=$value['fid'];
			$count=count($aids);
			$aids[0]=0;
			for($i=1;$i<$count;$i++){
				$row['jsprice']+=$dprices2[$row['pid']."_".$aids[$i]."_".$aids[$i-1]];
			}
			$r[]=$row;
		}
	}
	return $r;
}
// if(count($pid_arr)){
	// $sql_apply_did = "SELECT p.id,p.apply_did,l.title,l.id as lid FROM uu_products p LEFT JOIN uu_jq_ticket t ON p.id=t.pid LEFT JOIN uu_land l ON t.landid=l.id WHERE p.id IN(".implode(',',$pid_arr).") AND p.apply_limit=1 AND p.p_status IN(0,2,3,4,5)";
	// $GLOBALS['le']->query($sql_apply_did);
	// $fxIds = array();
	// while($list = $GLOBALS['le']->fetch_assoc()){
		// $fxIds[] = $list['lid'];
	// }
	// if(count($fxIds)){
		// $str = "SELECT id, title,p_type,getPY(title,1) as py FROM uu_land "
			// ."WHERE status=1 AND id IN (".implode(',',$fxIds).") ORDER BY py ASC";
		// $GLOBALS['le']->query($str);
		// while($row=$GLOBALS['le']->fetch_assoc()) {
			// $prods['1'][] = $row;
		// }
	// }
// }

$tmp = array();
$sortType='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
for($i=0;$i<strlen($sortType);$i++){
	foreach($prods[1] as $k=>$val){
		if($sortType[$i]==$val['py']){
			$tmp[]=$val;
			unset($prods[1][$k]);
		}
	}
}
$prods[1]=$tmp;
// print_r($prods[1]);
?>
<link rel="stylesheet" href="css/aliceui.min.css">
<link rel="stylesheet" href="css/resetui.css">
<link rel="stylesheet" href="css/prod_package.css?v=1010">
<link rel="stylesheet" href="css/prod_package_f.css?v=1010">
<div class="wrap">
    <div class="left">
        <div id="prod_result" class="list">
            <?foreach($prods as $key=>$prod){?>
            <ul id="prod_r<?=$key?>" class="prod_list">
                <?foreach($prod as $p){?>
                    <li class="<?=$p['p_type']?>">
                        <label>
                            <input type="checkbox" class="chkbox" data-title="<?=$p['title']?>" data-aid="<?=$p['sapply_did']?>" value="<?=$p['lid']?>" name="p1[]"><?=$p['py']?>-<?=$p['title']?>
                        </label>
                    </li>
                <?}?>
            </ul>
            <?}?>
        </div>
        <div id="tickt_result"></div>
        <div class="btn">
            <button type="button" class="ui-button ui-button-lorange" id="submit">下一步</button>
        </div>
    </div>
    <div class="right">
        <h2>您选择的产品</h2>
        <ul class="selected" id="selected"></ul>
    </div>
</div>
<form id="hideFrm" action="<?=$post_url;?>" method="post">
    <input type="hidden" name="package_data"/>
</form>
<script src="js/json2.js"></script>
<script src='js/prod_package.js'></script>
<?include('common/footer.html');?>